package com.goldgov.code.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.code.dao.ModuleDao;
import com.goldgov.code.json.FormField;
import com.goldgov.code.json.FormField.ControlType;
import com.goldgov.code.json.FormField.Validation;
import com.goldgov.code.json.ListField;
import com.goldgov.code.json.Operation;
import com.goldgov.code.json.Operation.DropdownOperation;
import com.goldgov.code.json.RootObj;
import com.goldgov.code.json.SearchFormField;
import com.goldgov.code.json.SelectFormField;
import com.goldgov.code.meta.Constraint;
import com.goldgov.code.meta.FieldInfo;
import com.goldgov.code.meta.ModuleInfo;
import com.goldgov.code.utils.StringUtils;

@RestController
@RequestMapping("/kdesign")
public class KdesignController {
	
//	private Log logger = LogFactory.getLog(getClass());
	
    @Autowired
    private ModuleDao moduleDao;
	
	@GetMapping("/config")
	public RootObj userLogin(@RequestParam("id") String id,@RequestParam("version") String version,@RequestParam("showModel") Integer showModel,@RequestParam(name="requestPrefix",required=false) String requestPrefix,HttpServletRequest request){
		RootObj rootObj = new RootObj(id);
		rootObj.setVersion(version);
		rootObj.setShowModel(showModel);
		
		ModuleInfo module = moduleDao.getModule(id);
		FieldInfo[] fields = module.getFields();
		rootObj.setCanSort(canSort(fields));
		
		requestPrefix = requestPrefix == null ? "" : requestPrefix;
		
		rootObj.setRequestUrl(requestPrefix + "/" + StringUtils.lowerFirstChar(module.getName()));
		
		boolean hasTree = hasTree(fields);
		rootObj.setHasTree(hasTree);
		if(hasTree) {
			rootObj.setTreeUrl(rootObj.getRequestUrl()+"Tree");
		}
		
		FormField[] formItems = new FormField[fields.length];
		List<ListField> listFields = new ArrayList<>();
		List<SearchFormField> searchItems = new ArrayList<>();
		
		Integer maxLength = null;
		String formatStr = null;
		for (int i = 0; i < fields.length; i++) {
			FieldInfo fieldInfo =  fields[i];
			
			Constraint constraint = fieldInfo.getConstraint();
			
			Validation format = null;
			ControlType controlType = null;
			boolean isListField = false;
			if(constraint != null) {
				if(constraint.getFormat() != null && !"".equals(constraint.getFormat())) {
					format = Enum.valueOf(Validation.class, constraint.getFormat());
				}
				if(constraint.getControlType() != null && !"".equals(constraint.getControlType())) {
					controlType = Enum.valueOf(ControlType.class, constraint.getControlType());
				}
				if(constraint.getQueryType() != null) {
					searchItems.add(new SearchFormField(fieldInfo.getName(),fieldInfo.getExplain(),controlType));
				}
				maxLength = constraint.getLength();
				formatStr = constraint.getFormat();
				isListField = constraint.isListField();
			}
			
			if(isListField) {
				listFields.add(new ListField(fieldInfo.getName(),fieldInfo.getExplain(),controlType));
			}
			
			if(controlType == ControlType.RemoteSelect) {
				formItems[i] = new SelectFormField(fieldInfo.getName(),fieldInfo.getExplain(),controlType,format,isListField);
				((SelectFormField)formItems[i]).putData("code", "DEMO_CODE");
			}else {
				formItems[i] = new FormField(fieldInfo.getName(),fieldInfo.getExplain(),controlType,format,isListField);
			}
			if(formatStr == null) {
				formItems[i].setFormat(formatStr);
			}
			if(maxLength != null && maxLength.intValue() > 0) {
				formItems[i].setLength(maxLength);
			}
		}
		rootObj.setListFieldList(listFields.toArray(new ListField[0]));
		rootObj.setFieldList(formItems);
		rootObj.setSearchList(searchItems.toArray(new SearchFormField[0]));
		
		//固定写死
		Operation[] operates = new Operation[2];
		operates[0] = new Operation("新增","Plus","add");
		
		DropdownOperation dropdownOperation = new DropdownOperation("批量操作","store","dropChange");
		dropdownOperation.setType("Dropdown");
		dropdownOperation.addOptionItem("删除", "delete");
		operates[1] = dropdownOperation;
		rootObj.setOperateList(operates);
		return rootObj;
	}
	
	private boolean canSort(FieldInfo[] fields) {
		for (FieldInfo fieldInfo : fields) {
			if(fieldInfo.getField().equals("ORDER_NUM")) {
				return true;
			}
		}
		return false;
	}
	
	private boolean hasTree(FieldInfo[] fields) {
		int hasTree = 0;
		for (FieldInfo fieldInfo : fields) {
			if(fieldInfo.getField().equals("TREE_PATH")) {
				hasTree |= 2;
			}else if(fieldInfo.getField().equals("TREE_VALUE")) {
				hasTree |= 1;
			}
		}
		return (hasTree&3) == 3;
	}
}
